<?php

namespace App\Repositories;

use App\Models\Linkage;

class LinkageRepository
{
    /*
     * 当sort的值为9999时接口不显示
     */
    CONST DISABLE = 9999;
    /**
     * 根据名称获取联动
     *
     * @param string $name
     * @return mixed
     */
    public function getByName($name)
    {
        return Linkage::where('name', $name)->first();
    }

    /**
     * 根据ID获取联动
     *
     * @param int $id
     * @return mixed
     */
    public function getById($id)
    {
        return Linkage::find($id);
    }

    /**
     * 创建联动
     *
     * @param array $attributes
     * @return $this|\Illuminate\Database\Eloquent\Model
     */
    public function create(array $attributes)
    {
        $linkage = Linkage::create($attributes);
        return $linkage;
    }

    /**
     * 批量添加联动
     *
     * @param array $attributes
     * @return mixed
     * @author ysh <397088124@qq.com>
     */
    public function insert(array $attributes)
    {
        $linkage = Linkage::insert($attributes);
        return $linkage;
    }

    /**
     * 修改联动
     *
     * @param array $attributes
     * @param Linkage $linkage
     * @return bool
     */
    public function update(array $attributes, Linkage $linkage)
    {
        return $linkage->update($attributes);
    }

    /**
     * 删除联动
     *
     * @param Linkage $linkage
     * @throws \Exception
     */
    public function destroy(Linkage $linkage)
    {
        $linkage->delete();
    }

    /**
     * 获取联动分页列表
     *
     * @param array $condition
     * @param int $per_page
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
     */
    public function paginate($condition = [], $per_page = 20)
    {
        $query = Linkage::orderBy('sort', 'asc')->orderBy('id', 'asc');
        $linkage = $this->buildQueryCondition($query,$condition);
        return $linkage->paginate($per_page);
    }

    /**
     * 获取指定组的所有联动
     *
     * @param int $category
     * @return mixed
     */
    public function getByCategory($category)
    {
        return Linkage::where('category', $category)->orderBy('sort', 'asc')->orderBy('id', 'asc')->get();
    }


    /**
     * /**
     * 获取所有联动
     * @param array $condition
     * @return mixed
     * @author ysh <397088124@qq.com>
     */
    public function all($condition = [])
    {
        $query = Linkage::orderBy('sort', 'asc')->orderBy('id', 'asc');
        $linkage = $this->buildQueryCondition($query,$condition);

        return $linkage->get();
    }

    /**
     * 查询构建
     *
     * @param $query
     * @param $condition
     * @return mixed
     * @author ysh <397088124@qq.com>
     */
    private function buildQueryCondition($query,$condition)
    {
        if (isset($condition['category'])) {
            $query->where('category', $condition['category']);
        }
        if (isset($condition['pid'])) {
            $query->where('pid', $condition['pid']);
        }
        return $query;
    }

    public function allForCache($condition = [])
    {
        $linkage = Linkage::orderBy('category', 'asc')->orderBy('pid', 'asc')->orderBy('sort', 'asc')->orderBy('id', 'asc');
        return $linkage->where($condition)->where('sort','<>',self::DISABLE)->get(['id','name','value','extend','keywords','pid']);
    }
}
